//
//  MKTool+DatePicker.m
//  Mar26_时间选择器
//
//  Created by Mike on 15-4-15.
//  Copyright (c) 2015年 galaxy. All rights reserved.
//

#import "MKToolDatePicker.h"
#import <UIKit/UIDatePicker.h>
#import <UIKit/UIScreen.h>
#import <UIKit/UIToolbar.h>

@interface MKToolDatePicker ()
@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) UIToolbar *toolBar;
@property (nonatomic, weak) UITextField *textField;
@end
@implementation MKToolDatePicker

- (UIDatePicker *)datePicker{
    if (!_datePicker) {
        
        // 先创建dataPicker
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, width, 180)];
        NSDate *nowdate=[NSDate date];
        _datePicker.maximumDate=nowdate;
        _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh-Hans"];
        _datePicker.datePickerMode = UIDatePickerModeDate;
        [_datePicker setBackgroundColor:[UIColor whiteColor]];
    }
    return _datePicker;
}
- (UIToolbar *)toolBar{
    if (!_toolBar) {
        // 输入的附属view
        _toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
        [_toolBar setBarTintColor:[UIColor whiteColor]];
//        UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:self action:@selector(previous)];
//        UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:self action:@selector(next)];
//        UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(done)];
        [item4 setTintColor:[UIColor blackColor]];
        _toolBar.items = @[ item4];
    }
    return _toolBar;
}
- (void)datePickerOnTextField:(UITextField *)textField{
    self.textField = textField;
    textField.inputView = self.datePicker;
    
    textField.inputAccessoryView = self.toolBar;
}
- (void)done{
    [self.textField endEditing:YES];
    if ([self.delegate respondsToSelector:@selector(toolDatePicker:onView:didClickedDoneWithDate:)]) {
        [self.delegate toolDatePicker:self onView:self.textField didClickedDoneWithDate:self.datePicker.date];
    }    
}
- (void)previous{

}
- (void)next{

}
@end
